package br.com.adimoveis.ui.cliente;

import java.util.Arrays;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import br.com.adimoveis.ui.AbstractView;
import br.com.adimoveis.ui.TableRenderer;
import br.com.adimoveis.util.ui.SpinnerModel;

public class ClienteView extends AbstractView implements IClienteView{

	private ProcuraPanel panel = new ProcuraPanel();
	
	public void enableForm(boolean enabled) {
		panel.andarLst.setEnabled(enabled);
		panel.bairroLst.setEnabled(enabled);
		panel.aparenciaLst.setEnabled(enabled);
		panel.metragemTf.setEnabled(enabled);
		panel.quartoTf.setEnabled(enabled);
		panel.tipoLst.setEnabled(enabled);
		panel.vagaTf.setEnabled(enabled);
		panel.valorTf.setEnabled(enabled);
		panel.suiteTf.setEnabled(enabled);
		
	}

	
	public JComponent getComponent() {
		return panel;
	}

	
	public Icon getIcon() {
		return new ImageIcon(ClassLoader.getSystemResource("contact-new.png"));
	}

	
	public String getTitle() {
		return "Cadastro";
	}


	public void setAndar(Object[] values, Object[] selected) {
		panel.andarLst.addElements(Arrays.asList(values), Arrays.asList(selected));
		
	}


	public void setBairros(Object[] values, Object[] selected) {
		panel.bairroLst.addElements(Arrays.asList(values), Arrays.asList(selected));
		
	}


	public void setTipos(Object[] values, Object[] selected) {
		panel.tipoLst.addElements(Arrays.asList(values), Arrays.asList(selected));
		
	}


	public void setEstados(Object[] values, Object[] selected) {
		panel.aparenciaLst.addElements(Arrays.asList(values), Arrays.asList(selected));
		
	}


	public String getMetragem() {
		return panel.metragemTf.getText();
	}


	public int getQuartos() {
		return (Integer)panel.quartoTf.getValue();
	}


	public Object[] getSelectedAndares() {
		return panel.andarLst.getSelectedElements().toArray();
	}


	public Object[] getSelectedBairros() {
		return panel.bairroLst.getSelectedElements().toArray();
	}


	public Object[] getSelectedEstados() {
		return panel.aparenciaLst.getSelectedElements().toArray();
	}


	public Object[] getSelectedTiposs() {
		return panel.tipoLst.getSelectedElements().toArray();
	}


	public int getVagas() {
		return (Integer)panel.vagaTf.getValue();
	}


	public String getValor() {
		return panel.valorTf.getText();
	}


	public void setMetragem(String metragem) {
		panel.metragemTf.setText(metragem);
	}


	public void setQuartos(int quartos) {
		SpinnerModel model = (SpinnerModel) panel.quartoTf.getModel();
		model.setValue(quartos);
	}


	public void setVagas(int vagas) {
		SpinnerModel model = (SpinnerModel) panel.vagaTf.getModel();
		model.setValue(vagas);
	}


	public void setValor(String valor) {
		panel.valorTf.setText(valor);
	}


	public int getSuites() {
		return (Integer)panel.suiteTf.getValue();
	}


	public void setSuites(int suites) {
		SpinnerModel model = (SpinnerModel) panel.suiteTf.getModel();
		model.setValue(suites);
	}


	public void setTableModel(TableModel clientemodel) {
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(clientemodel);
		panel.clienteTable.setModel(clientemodel);
		panel.clienteTable.setRowSorter(sorter);
		panel.clienteTable.revalidate();
	}


	public void showErroMessage(String message) {
		JOptionPane.showMessageDialog(new JFrame(), message,"Erro",JOptionPane.ERROR_MESSAGE);
	}


	public void selectValor() {
		panel.valorTf.requestFocus();
		panel.valorTf.select(0, panel.valorTf.getText().length());
	}


	public void selectMetragem() {
		panel.metragemTf.requestFocus();
		panel.metragemTf.select(0, panel.metragemTf.getText().length());
	}

	public int getSelectedIndex() {
		return panel.clienteTable.getSelectedRow();
	}


	public void setSelectedIndex(int selectedIndex) {
		panel.clienteTable.getSelectionModel().setSelectionInterval(selectedIndex, selectedIndex);
	}


	public void addSelectionListener(ListSelectionListener listSelectionListener) {
		panel.clienteTable.getSelectionModel().addListSelectionListener(listSelectionListener);
		
	}


	public Object getValueAt(int index, int i) {
		return panel.clienteTable.getValueAt(index, i);
	}


	public void setTableRender(TableRenderer tableRenderer) {
		for(int idx=0; idx< panel.clienteTable.getColumnCount(); idx++){
			TableColumn col = panel.clienteTable.getColumnModel().getColumn(idx);
			col.setCellRenderer(new TableRenderer());
		}
		panel.clienteTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}


	public void addChangeListener(ChangeListener changeListener) {
		panel.quartoTf.addChangeListener(changeListener);
		panel.suiteTf.addChangeListener(changeListener);
		panel.vagaTf.addChangeListener(changeListener);
	}


	public void setSuiteMaxVal(int suites) {
		SpinnerModel model = (SpinnerModel) panel.suiteTf.getModel();
		model.setMaxValue(suites);
	}


	public int getRowCount() {
		return panel.clienteTable.getRowCount();
	}

}
